import drawCSS from "../style/draw.css";
import modalCSS from "../style/modal.css";
import {GET_AGREEMENT, POST} from "../utils/network";
import Constant from "../constant";

export default class Base {

    /**
     * 构造参数
     * @param
     *  agreementNo
     *  version
     * @param options
     *          title
     *          titleColor
     *          showCancel
     *          confirmText
     *          confirmBackGroundColor
     *          confirmTextColor
     *          cancelText
     *          cancelBackGroundColor
     *          cancelTextColor
     *          forceRead
     *          forceReadTime
     *          timeout
     *          needSign
     *          userId
     *
     * @param type
     * @param onConfirm
     */
    constructor(agreementNo, title, version,  options = {}, type, onStart, onConfirm, onClose, onError) {
        this.agreementNo = agreementNo;
        this.title = options.title ? options.title : "";
        this.version = version ? version : "";
        this.titleColor = options.titleColor ? options.titleColor : "#000";
        this.confirmText = options.confirmText ? options.confirmText : "我知道了";
        this.confirmBackGroundColor = options.confirmBackGroundColor ? options.confirmBackGroundColor : "#2879ff";
        this.confirmTextColor = options.confirmTextColor ? options.confirmTextColor: "white";
        this.showCancel = options.showCancel ? options.showCancel : true;
        this.cancelText = options.cancelText ? options.cancelText : "取消";
        this.cancelBackGroundColor = options.cancelBackGroundColor ? options.cancelBackGroundColor : "#efefef";
        this.cancelTextColor = options.cancelTextColor ? options.cancelTextColor : "black";
        this.timeout = options.timeout ? options.timeout : "1000";
        this.forceRead = options.forceRead ? options.forceRead : false;
        this.forceReadTime = options.forceReadTime ? options.forceReadTime : 10;
        this.type = type;
        this.needSign = options.needSign ? options.needSign : false;
        this.userId = options.userId ? options.userId : "";
        this.onConfirm = onConfirm;
        this.onClose = onClose;
        this.onError = onError;
    }

    /**
     * 初始化方法
     */
    init() {
        return new Promise((resolve => {
            this.agreementElement = document.createElement("div");
            this.agreementElement.setAttribute("id","sdk__agreementSdk");
            this.agreementElement.setAttribute("class","sdk__agreement_sdk_class");
            this.$style = document.createElement('style');
            this.$style.type = "text/css";
            this.content;
            if (this.type === "modal") {
                this.$style.textContent = modalCSS;
                this.content = document.createElement("div");
                this.content.setAttribute("id","sdk__agreementModalContent");
                this.content.setAttribute("class","sdk__agreement_modal_content");
            } else {
                this.$style.textContent = drawCSS;
                this.content = document.createElement("div");
                this.content.setAttribute("id","sdk__agreementDrawContent");
                this.content.setAttribute("class","sdk__agreement_draw_content");
            }
            this.agreementElement.append(this.content);
            document.body.append(this.agreementElement);
            document.head.appendChild(this.$style);
            resolve();
        }))
    }

    /**
     * 点击确认框
     */
    confirm() {

    }

    /**
     * 弹窗关闭
     */
    close() {

    }

    /**
     * 释放
     */
    dispose() {
        this.agreementElement.remove();
    }

    initErrorTips() {
        const errorDiv = document.createElement("div");
        errorDiv.style.position = "absolute";
        errorDiv.style.width = "200px";
        errorDiv.style.height = "200px";
        errorDiv.style.left = "50%";
        errorDiv.style.top = "50%";
        errorDiv.style.transform = "translate(-50%, -50%)";
        errorDiv.style.display = "flex";
        errorDiv.style.flexDirection = "column";
        errorDiv.style.alignItems = "center";
        const divCicleElement = document.createElement("div");
        divCicleElement.style.width = "50px";
        divCicleElement.style.height = "50px";
        divCicleElement.style.borderRadius = "50%";
        divCicleElement.style.background = "#f76160";
        divCicleElement.style.fontSize = "30px";
        divCicleElement.innerHTML ="!";
        divCicleElement.style.fontWeight = "500px";
        divCicleElement.style.color = "white";
        divCicleElement.style.display = "flex";
        divCicleElement.style.alignItems = "center";
        divCicleElement.style.justifyContent = "center";
        divCicleElement.style.padding = "5px";
        const spanElement = document.createElement("span");
        spanElement.innerHTML ="网络异常，请稍后重试";
        spanElement.style.marginTop = "30px";
        errorDiv.append(divCicleElement);
        errorDiv.append(spanElement);
        return errorDiv.outerHTML;
    }

    sign() {
        let data = {
            userId: this.userId,
            agreementNo: this.agreementNo,
            version: this.version
        };
        console.log('signdata:' + JSON.stringify(data));
        POST(ENV_CONFIG.REQUEST_ADDRESS, Constant.SIGN, data, this.timeout).then((resp) => {
            // this.loading.remove();
            // typeof this.onConfirm === "function" && this.onConfirm();
        }, (error) => {

            // this.loading.remove();
            typeof this.onError === "function" && this.onError();
        });
    }
}